import Base from "./base";
import axios from "axios";
import SocketsXinghuo from "../../utils/chatxinghuo";

export default class extends Base {
  async infoAction() {
    console.log("什么鬼");
    console.log(this.ctx.state);
    const userId = this.ctx.state.userId;
    const memberService = think.service("memberService");
    const user = await memberService.info(userId);

    if (think.isEmpty(user)) {
      return this.fail("获取用户信息失败");
    } else {
      return this.success(user);
    }
  }
  async bindphoneAction() {
    const code = this.post("code");
    console.log(code);
    const weixinMiniConfig = this.config("weixinMini") || [];
    const wechatService = think.service("wechat", "api");
    const phoneInfo = await wechatService.getCodePhone(code, weixinMiniConfig);
    if (think.isEmpty(phoneInfo) || !phoneInfo.phoneNumber) {
      return this.fail(phoneInfo);
    }
    const userId = this.ctx.state.userId;
    const $MemberModel = think.model("member");
    const $pk = $MemberModel.pk;
    const data = await $MemberModel
      .where({ [$pk]: userId })
      .update({ phone: phoneInfo.phoneNumber });
    console.log(data);
    if (data === 1) {
      return this.success({
        phone: phoneInfo.phoneNumber,
      });
    } else {
      return this.fail("修改失败");
    }
  }

  
  async qrcodeAction() {
    const wechatService = think.service("wechat", "api");
    const weixinMiniConfig = this.config("weixinMini") || [];
    const data = await wechatService.getWxaqrcode(weixinMiniConfig);

    return this.success(data);
  }
 
}
